#coding:utf-8
__author__ = 'zhaoyoucai'

import requests
import hashlib
import time
import json
import urllib

class SMTAPI:
    api_root = 'http://120.76.134.53:8080'
    nameList = ['renewExpire','findAeProductStatusById','getRemainingWindows','editSingleSkuStock','editMultilanguageProduct','createProductGroup',
                'getSizeChartInfoByCategoryId','setSizeChart','getWindowProducts','editProductCategoryAttributes','setGroups','queryProductGroupIdByProductId',
                'getProductGroupList','editProductCidAttIdSku','editSimpleProductFiled','getAtributeMissingProductList','claimTaobaoProducts4API',
                'setShopwindowProduct','queryPromiseTemplateById','listTbProductByIds','findAeProductDetailModuleListByQurey','findAeProductModuleById',
                'onlineAeProduct','offlineAeProduct','createoffline','postAeProduct','getExpressAccess_token','refreshProductsByCompany','refreshOrdersByCompany',
                'getLogisticsSellerAddresses','getOnlineLogisticsInfo','createoffline','findOfflineEpcode','getOfflinePDFStream','sellerShipment',
                'getOnlineLogisticsServiceListByOrderId','createWarehouseOrder','getPrintInfo']

    paramsList = ['store_id','user_id','productId','skuId','stock','locale','subject','detail','name','parentId','categoryId','sizechartId','productCategoryAttributes',
                'groupIds','productSkus','productProperties','fiedName','fiedvalue','productIds','url','productIdList','templateId','moduleStatus','eType','page','mType',
                'moduleId','internationalLogisticsId','jsonData','orderId','weight','length','width','height','serviceName','logisticsNo','sendType','trackingWebsite','Epcode']

    #1.1延长商品有效期
    def renewExpire(self,store_id,productId):
        path = '/erp/api_product/renewExpire'
        r = requests.get(self.api_root+path,params={'store_id': store_id, 'productId': productId})

        return r.content

    #1.2查询商品状态
    def findAeProductStatusById(self,store_id,productId):
        path = '/erp/api_product/findAeProductStatusById'
        r = requests.get(self.api_root+path,params={'store_id': store_id, 'productId': productId})

        return r.content

    #1.3获取卖家可用橱窗数
    def getRemainingWindows(self,store_id):
        path = '/erp/api_product/getRemainingWindows'
        r = requests.get(self.api_root+path,params={'store_id': store_id})

        return r.content

    #1.4编辑商品单个SKU库存
    def editSingleSkuStock(self,store_id,productId,skuId,stock):
        path = '/erp/api_product/editSingleSkuStock'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'productId':productId,'skuId':skuId,'stock':stock})

        return r.content

    #1.5编辑商品的多语言标题或详描
    def editMultilanguageProduct(self,store_id,productId,locale,subject,detail):
        path = '/erp/api_product/editMultilanguageProduct'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'productId':productId,'locale':locale,'subject':subject,'detail':detail})

        return r.content

    #1.6创建产品分组
    def createProductGroup(self,store_id,name,parentId):
        path = '/erp/api_product/createProductGroup'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'name':name,'parentId':parentId})

        return r.content

    #1.7根据类目id获得适用的尺码表信息列表
    def getSizeChartInfoByCategoryId(self,store_id,categoryId):
        path = '/erp/api_product/getSizeChartInfoByCategoryId'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'categoryId':categoryId})

        return r.content

    #1.8修改商品所引用的尺码模板
    def setSizeChart(self,store_id,productId,sizechartId):
        path = '/erp/api_product/setSizeChart'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'productId':productId,'sizechartId':sizechartId})

        return r.content

    #1.9卖家橱窗使用详情查询
    def getWindowProducts(self,store_id):
        path = '/erp/api_product/getWindowProducts'
        r = requests.get(self.api_root+path,params={'store_id': store_id})

        return r.content

    #1.10编辑商品类目属性
    def editProductCategoryAttributes(self,store_id,productId,productCategoryAttributes):
        path = '/erp/api_product/editProductCategoryAttributes'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'productId':productId,'productCategoryAttributes':productCategoryAttributes})

        return r.content

    #1.11商品分组设置
    def setGroups(self,store_id,productId,groupIds):
        path = '/erp/api_product/setGroups'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'productId':productId,'groupIds':groupIds})

        return r.content

    #1.12查询指定商品ID所在产品分组
    def queryProductGroupIdByProductId(self,store_id,productId):
        path = '/erp/api_product/queryProductGroupIdByProductId'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'productId':productId})

        return r.content

    #1.13查询指定商品ID所在产品分组
    def getProductGroupList(self,store_id):
        path = '/erp/api_product/getProductGroupList'
        r = requests.get(self.api_root+path,params={'store_id': store_id})

        return r.content

    #1.14编辑产品类目、属性、sku
    def editProductCidAttIdSku(self,store_id,productId,categoryId,productSkus,productProperties):
        path = '/erp/api_product/editProductCidAttIdSku'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'productId':productId,'categoryId':categoryId,'productSkus':productSkus,'productProperties':productProperties})

        return r.content

    #1.15编辑商品的单个字段
    def editSimpleProductFiled(self,store_id,productId,fiedName,fiedvalue):
        path = '/erp/api_product/editSimpleProductFiled'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'productId':productId,'fiedName':fiedName,'fiedvalue':fiedvalue})

        return r.content

    #1.16获取属性需要优化的商品列表
    def getAtributeMissingProductList(self,store_id,productIds):
        path = '/erp/api_product/getAtributeMissingProductList'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'productIds':productIds})

        return r.content

    #1.17通过淘宝产品的url进行单品认领
    def claimTaobaoProducts4API(self,store_id,url):
        path = '/erp/api_product/claimTaobaoProducts4API'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'url':url})

        return r.content

    #1.18商品橱窗设置
    def setShopwindowProduct(self,store_id,productIdList):
        path = '/erp/api_product/setShopwindowProduct'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'productIdList':productIdList})

        return r.content

    #1.19服务模板查询
    def queryPromiseTemplateById(self,store_id,templateId):
        path = '/erp/api_product/queryPromiseTemplateById'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'templateId':templateId})

        return r.content

    #1.20获取淘宝原始产品信息
    def listTbProductByIds(self,store_id,productIds):
        path = '/erp/api_product/listTbProductByIds'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'productIds':productIds})

        return r.content

    #1.21查询信息模板列表
    def findAeProductDetailModuleListByQurey(self,store_id,moduleStatus,mType,page):
        path = '/erp/api_product/findAeProductDetailModuleListByQurey'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'moduleStatus':moduleStatus,'type':mType,'page':page})

        return r.content

    #1.22查询单个信息模板详情
    def findAeProductModuleById(self,store_id,moduleId):
        path = '/erp/api_product/findAeProductDetailModuleListByQurey'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'moduleId':moduleId})

        return r.content

    #1.23商品上架
    def onlineAeProduct(self,store_id,productIds):
        path = '/erp/api_product/onlineAeProduct'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'productIds':productIds})

        return r.content

    #1.24商品下架
    def offlineAeProduct(self,store_id,productIds):
        path = '/erp/api_product/offlineAeProduct'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'productIds':productIds})

        return r.content

    #1.25商品发布
    def postAeProduct(self,store_id,jsonData):
        path = '/erp/api_product/postAeProduct'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'json':jsonData})

        return r.content


    ##################
    # 系统接口
    ##################
    #2.1更新物流公司每个店铺access_token
    def getExpressAccess_token(self):
        path = '/erp/api_auth/getExpressAccess_token'
        r = requests.get(self.api_root+path)

        return r.content

    #2.2更新物流公司每个店铺的商品信息
    def refreshProductsByCompany(self):
        path = '/erp/api_product/refreshByCompany'
        r = requests.get(self.api_root+path)

        return r.content

    #2.3刷新公司下所有店铺的订单信息
    def refreshOrdersByCompany(self):
        path = '/erp/api_order/refreshByCompany'
        r = requests.get(self.api_root+path)

        return r.content

    ##################
    # 订单接口
    ##################
    #3.1获取卖家地址(获取店铺的发货及退货地址)
    def getLogisticsSellerAddresses(self,store_id):
        path = '/erp/api_order/getLogisticsSellerAddresses'
        r = requests.get(self.api_root+path,params={'store_id': store_id})

        return r.content

    #3.2获取线上发货物流订单信息
    def getOnlineLogisticsInfo(self,store_id,orderId):
        path = '/erp/api_order/getOnlineLogisticsInfo'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'orderId':orderId})

        return r.content


    #3.3获取线下发货物流订单信息
    def createoffline(self,store_id,jsonData):
        path = '/erp/api_order/createoffline'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'json':jsonData})
        return r.content
        #requests.get(self.api_root+path,params={'store_id': store_id,'json':jsonData})
        #print(self.api_root+path)
        #requests.get('http://127.0.0.1:8900/erp/api/testSend/',params={'store_id': store_id,'json':jsonData})


    #3.4查询线下发货订单
    def findOfflineEpcode(self,orderId):
        path = '/erp/api_order/findOfflineEpcode'
        r = requests.get(self.api_root+path,params={'orderId': orderId})

        return r.content

    #3.5获取线下发货pdf面单
    def getOfflinePDFStream(self,Epcode,eType):
        path = '/erp/api_order/getOfflinePDFStream'
        r = requests.get(self.api_root+path,params={'Epcode': Epcode,'type':eType})

        return r.content

    #3.6填写发货通知
    def sellerShipment(self,store_id,serviceName,logisticsNo,sendType,orderId,trackingWebsite=''):
        path = '/erp/api_order/sellerShipment'
        #r = requests.get(self.api_root+path,params={'store_id': store_id,'serviceName':serviceName,'logisticsNo':logisticsNo,'sendType':sendType,'orderId':orderId,'trackingWebsite':trackingWebsite})
        r = requests.get('http://127.0.0.1:8900/erp/api/testSend/',params={'store_id': store_id,'serviceName':serviceName,'logisticsNo':logisticsNo,'sendType':sendType,'orderId':orderId,'trackingWebsite':trackingWebsite})
        return r.content

    #3.7根据交易订单获取线上发货物流服务列表
    def getOnlineLogisticsServiceListByOrderId(self,store_id,orderId,weight,length,width,height):
        path = '/erp/api_order/getOnlineLogisticsServiceListByOrderId'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'weight':weight,'length':length,'width':width,'orderId':orderId,'height':height})

        return r.content

    #3.8创建线上发货物流订单信息
    def createWarehouseOrder(self,store_id,jsonData):
        path = '/erp/api_order/createWarehouseOrder'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'json':jsonData})
        return r.content
        #print(self.api_root+path)
        #requests.get('http://127.0.0.1:8900/erp/api/testSend/',params={'store_id': store_id,'json':jsonData})

    #3.9获取线上发货标签(获取打印免单base64)
    def getPrintInfo(self,store_id,internationalLogisticsId):
        path = '/erp/api_order/getPrintInfo'
        r = requests.get(self.api_root+path,params={'store_id': store_id,'internationalLogisticsId':internationalLogisticsId})

        return r.content



class LOGISTICS:

    api_root = 'http://120.76.134.53:8080'
    nameList = ['calculate']

    paramsList = ['country','weight','IS_ELECTRIC','lengths','widths','heights','jinghuojia','profit']

    def calculate(self,country,weight,IS_ELECTRIC=None,lengths=None,widths=None,heights=None,jinghuojia=None,profit=None):
        path = '/tjcrm/api_logistics/calculate.do'

        params = dict()

        params['country'] = country
        params['weight'] = weight

        if IS_ELECTRIC:
            params['IS_ELECTRIC'] = IS_ELECTRIC
        if lengths:
            params['lengths'] = lengths
        if widths:
            params['widths'] = widths
        if heights:
            params['heights'] = heights
        if jinghuojia:
            params['jinghuojia'] = jinghuojia
        if profit:
            params['profit'] = profit

        r = requests.get(self.api_root+path,params=params)

        return r.content


class TRANSLATOR:
    app_id = '20161031000031072'
    app_key = '_OP9_QNAB5p1kKwVNx1X'
    root = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
    available = True

    def baiduTranslate(self,word,to='zh'):
        result = dict()
        salt = int(time.time())
        signstr ='%s%s%d%s'%(self.app_id,word,salt,self.app_key)
        m = hashlib.md5()
        m.update(signstr)
        sign = m.hexdigest()

        data = {
            'q':word,
            'from':'auto',
            'to':to,
            'appid':self.app_id,
            'salt':salt,
            'sign':sign

        }

        #url = '%s?q=%s&from=auto&to=zh&appid=%s&salt=%d&sign=%s'%(self.root,word,self.app_id,salt,sign)
        url = '%s?%s'%(self.root,urllib.urlencode(data))

        r = requests.get(url)
        data = r.content

        data = json.loads(data)
        if data.has_key('trans_result'):
            result["result"] = data["trans_result"][0]["dst"]
            result["error"] = 0
        else:
            result["error"] = 1
            result["msg"] = data["error_code"]+':'+data["error_msg"]


        return result
